package com.quxiu.system.controller;

import java.util.List;
import javax.annotation.Resource;

import com.quxiu.common.core.utils.StringUtils;
import com.quxiu.common.core.utils.security.SecretUtil;
import com.quxiu.system.domain.SysApp;
import com.quxiu.system.domain.vo.PushDataResult;
import com.quxiu.system.service.ISysAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.system.domain.SysDataPushRecord;
import com.quxiu.system.service.ISysDataPushRecordService;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.web.page.TableDataInfo;

/**
 * 数据推送记录Controller
 *
 * @author GaoYang
 * @date 2024-01-19
 */
@Slf4j
@RestController
@RequestMapping("/dataPush")
public class SysDataPushRecordController extends BaseController {
    @Resource
    private ISysDataPushRecordService sysDataPushRecordService;
    @Resource
    private ISysAppService sysAppService;

    /**
     * 查询数据推送记录列表
     */
    @RequiresPermissions("system:dataPush:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDataPushRecord sysDataPushRecord) {
        startPage();
        List<SysDataPushRecord> list = sysDataPushRecordService.selectSysDataPushRecordList(sysDataPushRecord);
        return getDataTable(list);
    }

    /**
     * 获取数据推送记录详细信息
     */
    @RequiresPermissions("system:dataPush:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return success(sysDataPushRecordService.selectSysDataPushRecordByRecordId(recordId));
    }

    /**
     * 测试接收数据推送结果
     */
    @PostMapping(value = "/testSyncData")
    public PushDataResult testSyncData(@RequestParam(required = false) String flag, @RequestBody String data, @RequestHeader("appId") String appId) {
        PushDataResult result = new PushDataResult();
        if (StringUtils.isNotBlank(data) && "ok".equalsIgnoreCase(flag)) {
            SysApp app = sysAppService.selectSysAppByAppId(appId);
            if (app != null) {
                String dData = SecretUtil.decryptRsaForPublicKey(data, app.getPublicKey());
                log.info("测试接收数据推送结果：{}", dData);
                result.setStatus(1);
                return result;
            }
        }
        result.setStatus(0);
        result.setMsg("数据处理失败，请联系相关运维人员！");
        return result;
    }
}
